package com.bishe.common.controller;

import com.bishe.common.utils.MinIoUtil;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.bishe.common.Configuration.MinIoProperties;

@RestController
@RequiredArgsConstructor
@RequestMapping("/minio")
public class MinIoController {


    private final MinIoProperties minIoProperties;

    @PostMapping(value = "/upload")
    public String upload(@RequestParam("file") MultipartFile file) throws Exception {
        System.out.println("文件上传开始");
        String fileUrl = MinIoUtil.upload(minIoProperties.getBucketName(), file);
        return "文件下载地址：" + fileUrl;
    }

    @GetMapping(value = "/download")
    public void download(@RequestParam("fileName") String fileName, HttpServletResponse response) {
        MinIoUtil.download(minIoProperties.getBucketName(), fileName, response);
    }

    @GetMapping(value = "/delete")
    public String delete(@RequestParam("fileName") String fileName) {
        MinIoUtil.deleteFile(minIoProperties.getBucketName(), fileName);
        return "删除成功";
    }

}

